//
//  BKArchiveViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/5/27.
//  Copyright (c) 2015年 报考大学. All rights reserved.
//

#import "BKArchiveViewController.h"
#import "BKPopView.h"
#import "BKArchinveViewModel.h"
#import <MBProgressHUD.h>
#import "SecondViewController.h"

@interface BKArchiveViewController ()<BKPopViewDelegate>
@property (nonatomic ,strong) BKArchinveViewModel *viewModel;
@property (weak, nonatomic) IBOutlet UILabel *piciLabel;
@property (weak, nonatomic) IBOutlet UIImageView *cloudImageView;

@property (weak, nonatomic) IBOutlet UILabel *proveLabel;
@property (readonly ,nonatomic) NSDictionary *piciCode;
@end

@implementation BKArchiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _viewModel = [BKArchinveViewModel share];
    _piciCode = @{@"提前批":@"10",
                  @"本科一批":@"11",
                  @"本科二批":@"12",
                  @"本科三批":@"13",
                  @"高职专科":@"20"};
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(p_back:) name:@"archiveHome" object:nil];
    
    
    
    CABasicAnimation *cloudAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.x"];
    cloudAnimation.toValue = [NSNumber numberWithFloat:-300];
    
    cloudAnimation.duration = 30;
    cloudAnimation.repeatCount = 1000000;
    [_cloudImageView.layer addAnimation:cloudAnimation forKey:@"transform.translation.x"];
    
}

- (void)dealloc{

    
    [_cloudImageView.layer removeAllAnimations];
}

- (void)viewDidLayoutSubviews{

   
}

- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];;
     _cloudImageView.hidden = NO;
    
    UITabBarController *tabBarViewController = (UITabBarController *)[[[UIApplication sharedApplication] keyWindow] rootViewController];
    if ([tabBarViewController isKindOfClass:[UITabBarController class]]) {
        [tabBarViewController.tabBar setBarTintColor:[UIColor colorWithRed:57.0/255.0 green:99.0/255.0 blue:101/255.0 alpha:1.000]];
    }
   
}

- (void)viewWillDisappear:(BOOL)animated{

    _cloudImageView.hidden = YES;
    UITabBarController *tabBarViewController = (UITabBarController *)[[[UIApplication sharedApplication] keyWindow] rootViewController];
    [tabBarViewController.tabBar setBarTintColor:[UIColor colorWithRed:0.459 green:0.839 blue:0.863 alpha:1.000]];
    [super viewWillDisappear:animated];
}



- (IBAction)p_back:(id)sender {
    [_cloudImageView removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)showPiCi:(id)sender {
    
    BKPopView *popView = [[BKPopView alloc] initWithView:self.view];
    popView.dataArray = @[@"提前批",@"本科一批",@"本科二批",@"本科三批",@"高职专科"];
    popView.delegate = self;
    popView.tag = 101;
    [self.view addSubview:popView];
    [popView show];
}
- (IBAction)showProv:(id)sender {
    __weak BKArchiveViewController *weakSelf = self;
    self.view.userInteractionEnabled = NO;
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.labelText = @"加载省份";
    [hud show:YES];
    [_viewModel provWithSuccess:^(NSArray *dataArray) {
        [hud hide:YES];
        BKPopView *popView = [[BKPopView alloc] initWithView:weakSelf.view];
        popView.dataArray =dataArray;
        popView.delegate = weakSelf;
        popView.tag = 102;
        [weakSelf.view addSubview:popView];
        [popView show];
        weakSelf.view.userInteractionEnabled = YES;
        
    }
                        failure:^(NSString *message) {
                            [hud hide:YES];
                            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                            [alertView show];
                            
                            weakSelf.view.userInteractionEnabled = YES;
                        }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)bkPopView:(BKPopView *)bkPopView
      confirmItem:(NSString *)item{
    if(bkPopView.tag==101){
        _piciLabel.text = item;
        
    }
    if(bkPopView.tag==102){
        _proveLabel.text = item;
        
    }
    
    if ([_proveLabel.text length]>0 && [_piciLabel.text length]>0) {
        
        MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
        hud.labelText = @"为您找大学";
        [hud show:YES];
        NSDictionary *params = @{@"pici":_piciCode[_piciLabel.text],
                                 @"diqu":_proveLabel.text,
                                 @"page":@"1"};
        
        [_viewModel searchWithParams:params
                             success:^(NSArray *dataArray) {
                                 SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:NSStringFromClass([SecondViewController class]) bundle:nil];
                                 secondVC.dataArray = dataArray;
                                 secondVC.params = params;
                                 
                                 [self.navigationController pushViewController:secondVC animated:YES];
                                 
                                 
                             }
                             failure:^(NSString *message) {
                                 [hud hide:YES];
                                 UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                                 [alertView show];
                                 
                             }];
        
    }
    
}

@end
